<template>
  <div class="ele-body">
    <el-card shadow="never">
      <div slot="header" class="clearfix">
        <span>模板市场</span>
      </div>
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :columns="columns"
        :datasource="url"
        :max-height="defaultHeight"
        :selection.sync="selection"
        :parse-data="parseData"
        :where="where"
      >
        <!-- 表头工具栏 -->
        <template slot="toolbar">
          <el-form
            :model="where"
            class="ele-form-search"
            label-width="75px"
            @keyup.enter.native="reload"
            @submit.native.prevent
          >
            <div class="app-container">
              <div class="filter-container">
                <el-input
                  v-model="where.name"
                  class="filter-item"
                  clearable
                  placeholder="模板名称"
                  style="width: 240px"
                  @clear="reload"
                />
                <el-button
                  class="filter-item"
                  icon="el-icon-search"
                  type="primary"
                  @click="reload"
                  >搜索
                </el-button>
                <el-button
                  class="filter-item"
                  icon="el-icon-refresh"
                  style="margin-left: 2px"
                  @click="reset"
                  >重置
                </el-button>
              </div>
            </div>
          </el-form>
        </template>
        <!-- 图片列 -->
        <template slot="image" slot-scope="{ row }">
          <el-image
            style="width: 48px; height: 48px"
            :src="getImage(row.image)"
            :preview-src-list="previewSrcList(row.image)"
          >
            <div
              slot="error"
              style="
                display: flex;
                justify-content: center;
                align-items: center;
                width: 100%;
                height: 100%;
                background: #f5f7fa;
                color: #909399;
                font-size: 30px;
              "
            >
              <i class="el-icon-picture-outline"></i>
            </div>
          </el-image>
        </template>
        <!-- 操作列 -->
        <template slot="action" slot-scope="{ row }">
          <el-link
            :underline="false"
            icon="el-icon-setting"
            type="primary"
            @click="openEdit(row)"
            >应用此模板
          </el-link>
        </template>
      </ele-pro-table>
    </el-card>
    <!-- 编辑弹窗 -->
    <vue-edit
      :source="source"
      :site="siteList"
      :visible.sync="showEdit"
      @done="reload"
    />
  </div>
</template>

<script>
import { messageLoading } from "@/panel";
import VueEdit from "./vue-edit";

export default {
  name: "SystemTemplate",
  components: { VueEdit },
  data() {
    return {
      // 表格高度
      defaultHeight: 0,
      // 表格数据接口
      url: "/template/read",
      // 表格列配置
      columns: [
        {
          columnKey: "index",
          type: "index",
          width: 100,
          label: "序号",
          align: "center",
          showOverflowTooltip: true,
        },
        {
          prop: "name",
          label: "模板名称",
          sortable: "custom",
          showOverflowTooltip: true,
          minWidth: 110,
        },
        {
          prop: "image",
          slot: "image",
          label: "效果图",
          sortable: "custom",
          showOverflowTooltip: true,
          minWidth: 110,
        },
        {
          prop: "datetime",
          label: "创建时间",
          sortable: "custom",
          showOverflowTooltip: true,
          minWidth: 110,
          formatter: (row, column, cellValue) => {
            return this.$util.toDateString(cellValue);
          },
        },
        {
          prop: "remark",
          label: "模板备注",
          sortable: "custom",
          showOverflowTooltip: true,
        },
        {
          columnKey: "action",
          label: "操作",
          width: 200,
          resizable: false,
          slot: "action",
        },
      ],
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      source: null,
      // 是否显示编辑弹窗
      showEdit: false,
      // 站点列表
      siteList: [],
    };
  },
  created() {
    window.addEventListener("resize", this.getHeight);
    this.getHeight();
  },

  methods: {
    getImage(obj) {
      if (!obj || obj.length === 0) {
        return "";
      }
      return obj[0].url;
    },

    previewSrcList(obj) {
      if (!obj) {
        return [];
      }
      return obj.map((item) => item.url);
    },

    parseData(res) {
      if (res.code === 0) {
        this.siteList = res.site;
      }
      return {
        code: res.code,
        data: res.data,
        count: res.count,
        msg: res.msg,
      };
    },

    /*设置表格高度*/
    getHeight() {
      this.defaultHeight = window.innerHeight - 360;
    },

    /* 刷新表格 */
    reload(isPage) {
      let obj = { where: this.where };
      if (!isPage) {
        obj.page = 1;
      }
      this.$refs.table.reload(obj);
    },

    /* 重置搜索 */
    reset() {
      for (let key in this.where) {
        this.where[key] = null;
      }
      this.reload();
    },

    /* 显示编辑 */
    openEdit(row) {
      if (!this.$hasPermission("template:compile")) {
        return this.$message.error("你没有权限执行此操作!");
      }
      const loading = messageLoading("正在请求中..");
      this.$http
        .post("/template/exists", row)
        .then((res) => {
          loading.close();
          if (res.data.code === 0) {
            this.source = row.source;
            this.showEdit = true;
          } else {
            this.$message.error(res.data.msg);
          }
        })
        .catch((e) => {
          loading.close();
          this.$message.error(e.message);
        });
    },
  },
};
</script>

